package com.ruoyi.interview.service.impl;

import java.util.Date;
import java.util.List;


import com.ruoyi.common.core.domain.R;
import com.ruoyi.interview.domain.*;
import com.ruoyi.interview.domain.vo.TbClassVo;
import com.ruoyi.interview.mapper.*;
import com.ruoyi.interview.domain.vo.LoginVo;

import com.ruoyi.interview.domain.vo.TbStudentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.interview.mapper.TbInterviewMapper;
import com.ruoyi.interview.service.ITbInterviewService;

/**
 * 面试明细Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-05-27
 */
@Service
public class TbInterviewServiceImpl implements ITbInterviewService 
{
    @Autowired
    private TbInterviewMapper tbInterviewMapper;

    @Autowired
    private TbCorporationMapper tbCorporationMapper;

    @Autowired
    private TbRecordsMapper tbRecordsMapper;

    @Autowired
    private TbPostMapper tbPostMapper;

    /**
     * 查询面试明细
     * 
     * @param id 面试明细主键
     * @return 面试明细
     */
    @Override
    public TbInterview selectTbInterviewById(Long id)
    {
        return tbInterviewMapper.selectTbInterviewById(id);
    }

    /**
     * 查询面试明细列表
     * 
     * @param tbInterview 面试明细
     * @return 面试明细
     */
    @Override
    public List<TbInterview> selectTbInterviewList(TbInterview tbInterview)
    {
        return tbInterviewMapper.selectTbInterviewList(tbInterview);
    }

    /**
     * 新增面试明细
     * 
     * @param tbRecords 面试明细
     * @return 结果
     */
    @Override
    public int insertTbInterview(TbRecords tbRecords)
    {
        int code = 0;
        //判断公司是否存在
        TbCorporation tbCorporation =  tbCorporationMapper.selectTbCommentByName(tbRecords.getCorporationName(),tbRecords.getCorporationAddress());
        //如果公司存在
        if (tbCorporation != null){
            //获取公司id
            tbRecords.setCorporationId(tbCorporation.getId());

            //判断岗位是否存在
            TbPost tbPost   =  tbPostMapper.selectTbPostByName(tbCorporation.getId(),tbRecords.getPostName());
            //岗位不存在
            if (tbPost == null){
                //添加岗位
                TbPost tbPost1 = new TbPost();
                tbPost1.setPostName(tbRecords.getPostName());
                tbPost1.setCreatedTime(new Date());
                tbPost1.setCorporationId(tbCorporation.getId());
                code =  tbPostMapper.insertTbPost(tbPost1);

                //获取岗位id
                tbRecords.setPostId(tbPost1.getId());
            }else {
                //岗位存在,直接获取id
                tbRecords.setPostId(tbPost.getId());
            }

        }else {
            //公司不存在,添加公司
            TbCorporation tbCorporation1 = new TbCorporation();
            tbCorporation1.setCorporationAddress(tbRecords.getCorporationAddress());
            tbCorporation1.setCorporationName(tbRecords.getCorporationName());
            tbCorporation1.setCreateTime(new Date());
            code =  tbCorporationMapper.insertTbCorporation(tbCorporation1);
            //获取新公司id
            tbRecords.setCorporationId(tbCorporation1.getId());

            //添加岗位
            TbPost tbPost1 = new TbPost();
            tbPost1.setPostName(tbRecords.getPostName());
            tbPost1.setCreatedTime(new Date());
            tbPost1.setCorporationId(tbCorporation1.getId());
            code =  tbPostMapper.insertTbPost(tbPost1);

            //获取岗位id
            tbRecords.setPostId(tbPost1.getId());
        }

        //添加面试记录
        tbRecords.setCreatedTime(new Date());
        code = tbRecordsMapper.insertTbRecords(tbRecords);

        //添加面试详情

        for (TbInterview tbInterview : tbRecords.getInterviewList()) {
            tbInterview.setRecordId(tbRecords.getId());
            code = tbInterviewMapper.insertTbInterview(tbInterview);
        }
        return code;
    }

    /**
     * 修改面试明细
     * 
     * @param tbRecords 面试明细
     * @return 结果
     */
    @Override
    public int updateTbInterview(TbRecords tbRecords)
    {
        int code = 0;
        //判断公司是否存在
        TbCorporation tbCorporation =  tbCorporationMapper.selectTbCommentByName(tbRecords.getCorporationName(),tbRecords.getCorporationAddress());
        //如果公司存在
        if (tbCorporation != null){
            //获取公司id
            tbRecords.setCorporationId(tbCorporation.getId());

            //判断岗位是否存在
            TbPost tbPost   =  tbPostMapper.selectTbPostByName(tbCorporation.getId(),tbRecords.getPostName());
            //岗位不存在
            if (tbPost == null){
                //添加岗位
                TbPost tbPost1 = new TbPost();
                tbPost1.setPostName(tbRecords.getPostName());
                tbPost1.setCreatedTime(new Date());
                tbPost1.setCorporationId(tbCorporation.getId());
                code =  tbPostMapper.insertTbPost(tbPost1);

                //获取岗位id
                tbRecords.setPostId(tbPost1.getId());
            }else {
                //岗位存在,直接获取id
                tbRecords.setPostId(tbPost.getId());
            }

        }else {
            //公司不存在,添加公司
            TbCorporation tbCorporation1 = new TbCorporation();
            tbCorporation1.setCorporationAddress(tbRecords.getCorporationAddress());
            tbCorporation1.setCorporationName(tbRecords.getCorporationName());
            tbCorporation1.setCreateTime(new Date());
            code =  tbCorporationMapper.insertTbCorporation(tbCorporation1);
            //获取新公司id
            tbRecords.setCorporationId(tbCorporation1.getId());
        }

        //修改面试记录
        code = tbRecordsMapper.updateTbRecords(tbRecords);

        //删除面试详细
        code = tbInterviewMapper.deleteTbInterviewByRecordsId(tbRecords.getId());
        //添加面试详情
        for (TbInterview tbInterview : tbRecords.getInterviewList()) {
            tbInterview.setRecordId(tbRecords.getId());
            code = tbInterviewMapper.insertTbInterview(tbInterview);
        }
        return code;
    }

    /**
     * 批量删除面试明细
     * 
     * @param ids 需要删除的面试明细主键
     * @return 结果
     */
    @Override
    public int deleteTbInterviewByIds(Long[] ids)
    {
        return tbInterviewMapper.deleteTbInterviewByIds(ids);
    }

    /**
     * 删除面试明细信息
     * 
     * @param id 面试明细主键
     * @return 结果
     */
    @Override
    public int deleteTbInterviewById(Long id)
    {
        int i =0;
        i=tbInterviewMapper.deleteTbInterviewByRecordsId(id);
        i=tbInterviewMapper.deleteTbRecordsById(id);
        return i;
    }

    @Override
    public List<TbRecords> selectTbRecordsList(TbRecords tbRecords) {
        return tbInterviewMapper.selectTbRecordsList(tbRecords);
    }

    @Override
    public R interviewLogin(LoginVo loginVo) {
       TbStudentVo tbStudentVo =  tbInterviewMapper.interviewLogin(loginVo);
       if (tbStudentVo!=null){
           if (!loginVo.getStuMobile().equals(tbStudentVo.getStuMobile())){
               return R.fail(500,"手机号错误");
           }
        TbClassVo tbClassVo = tbInterviewMapper.getTbClassVo(tbStudentVo.getClassId());
        if (!loginVo.getPassword().equals(tbClassVo.getPassword())){
            return R.fail(500,"密钥错误");
        }
       }else {
           return R.fail(500,"不存在该学号");
       }
        return R.ok(tbStudentVo,"登录成功") ;
    }

    @Override
    public List<TbCorporation> findCorporation() {
        return tbInterviewMapper.findCorporation();
    }

    @Override
    public List<TbPost> findPost() {
        return tbInterviewMapper.findPost();
    }

    @Override
    public int saveComment(TbComment tbComment) {
        return tbInterviewMapper.saveComment(tbComment);
    }

    @Override
    public List<TbRecords> selectInfoList() {
        return tbInterviewMapper.selectInfoList();
    }
}
